package yhao.infra.feature.alipay.core.request.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import org.apache.commons.lang3.time.DateFormatUtils;
import yhao.infra.feature.alipay.core.request.BasePayRequest;

import java.util.Date;

/**
 * app支付接口2.0
 * @see <a href="https://docs.open.alipay.com/api_1/alipay.trade.app.pay/">更多细节</a>
 */
public class PayAppRequest extends BasePayRequest<AlipayTradeAppPayResponse> {
    private String product_code;//产品销售码
    private String time_expire;//绝对超时时间

    public PayAppRequest(String out_trade_no, String total_amount, String subject, Date empireTime, String url) {
        super(out_trade_no, total_amount, subject,url);
        this.product_code = "QUICK_MSECURITY_PAY";
        this.time_expire = DateFormatUtils.format(empireTime, "yyyy-MM-dd HH:mm");
    }

    public String getProduct_code() {
        return product_code;
    }

    public String getTime_expire() {
        return time_expire;
    }

    @Override
    protected AlipayTradeAppPayResponse doRequest() {
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        request.setBizContent(bizContent);
        request.setNotifyUrl(alipayConfig.getNotifyUrlPre()+this.getNotifyUrl());

        String method = logRequest(request);
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            return alipayClient.sdkExecute(request);
        } catch (AlipayApiException e) {
            logger.error(method+getOut_trade_no(), e.getMessage());
        }
        return null;
    }
}
